//
//  ViewController.m
//  Bowling
//
//  Created by iD Student on 7/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
{
        
    NSURL *url = [NSURL fileURLWithPath:@"/Users/Student/Desktop/Ali - iOS/Bowling/Skyrim_Theme2.mp3"];
    appAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    [appAudio play];
    
    
    NSURL *url2 = [NSURL fileURLWithPath:@"/Users/Student/Desktop/Ali - iOS/Bowling/fus.mp3"];
    ballAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:url2 error:nil];
    
    
    
    NSURL *url3 = [NSURL fileURLWithPath:@"/Users/Student/Desktop/Ali - iOS/Bowling/strike.wav"];
    strikeAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:url3 error:nil];
    
    
    NSURL *url4 = [NSURL fileURLWithPath:@"/Users/Student/Desktop/Ali - iOS/Bowling/bowling1.wav"];
    ballAudio2 = [[AVAudioPlayer alloc]initWithContentsOfURL:url4 error:nil];
    
    x=0;
     


    [super viewDidLoad];
}

- (void)viewDidUnload
{

   
    mPin = nil;
    ball = nil;
    r1Pin = nil;
    r2Pin = nil;
    r3Pin = nil;
    r4Pin = nil;
    l1Pin = nil;
    l2Pin = nil;
    l3Pin = nil;
    l4Pin = nil;
    score = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(void)moveRight:(id)sender
{
    if(ball.center.x < 270)
        ball.center = CGPointMake(ball.center.x + 20, ball.center.y);
    
}




-(void)moveLeft:(id)sender
{
    if(ball.center.x > 50)
        ball.center = CGPointMake(ball.center.x - 20, ball.center.y);
}



- (IBAction)goBall:(id)sender {


    timer=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animate) userInfo:nil repeats:YES];
    [self.view addSubview:ball];
    
    [ballAudio play];
    
    [ballAudio2 play];


}



-(void) animate
{
    

    if (ball.center.y > 20){
        ball.center = CGPointMake(ball.center.x, ball.center.y - 10);
        
    }
    
    if(ball.center.y<20){
        [timer invalidate];
        [self hidePins];
        
        ball.center= CGPointMake(165, 401);
        [self changelabel];

    }



if (CGRectIntersectsRect(ball.frame, l1Pin.frame)) {
    l1Pin.center=CGPointMake(l1Pin.center.x -20, l1Pin.center.y -20);
    l1PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];

}
if (CGRectIntersectsRect(ball.frame, l2Pin.frame)) {
    l2Pin.center=CGPointMake(l2Pin.center.x -20, l2Pin.center.y -20);
    l2PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];

}
if (CGRectIntersectsRect(ball.frame, l3Pin.frame)) {
    l3Pin.center=CGPointMake(l3Pin.center.x -20, l3Pin.center.y -20);
    l3PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];

}
if (CGRectIntersectsRect(ball.frame, l4Pin.frame)) {
    l4Pin.center=CGPointMake(l4Pin.center.x -20, l4Pin.center.y -20);
    l4PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];

}
if (CGRectIntersectsRect(ball.frame, mPin.frame)) {
    mPin.center=CGPointMake(mPin.center.x , mPin.center.y -5);
    mPinHit=TRUE;
    
    ballAudio.volume = 0.4; 
    [strikeAudio play];

}
if (CGRectIntersectsRect(ball.frame, r1Pin.frame)) {
    r1Pin.center=CGPointMake(r1Pin.center.x +20, r1Pin.center.y -20);
    r1PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];
}
if (CGRectIntersectsRect(ball.frame, r2Pin.frame)) {
    r2Pin.center=CGPointMake(r2Pin.center.x +20, r2Pin.center.y -20);
    r2PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];
    

}
if (CGRectIntersectsRect(ball.frame, r3Pin.frame)) {
    r3Pin.center=CGPointMake(r3Pin.center.x +20, r3Pin.center.y -20);
    r3PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];


}
if (CGRectIntersectsRect(ball.frame, r4Pin.frame)) {
    r4Pin.center=CGPointMake(r4Pin.center.x +20, r4Pin.center.y -20);
    r4PinHit=TRUE;
    
    ballAudio.volume = 0.4;
    [strikeAudio play];


}
    if (CGRectIntersectsRect(l1Pin.frame, l2Pin.frame)) {
        l1Pin.center=CGPointMake(l1Pin.center.x -20, l1Pin.center.y -20);
        l1PinHit=TRUE;

    }
    if (CGRectIntersectsRect(l2Pin.frame, l3Pin.frame)) {
        l2Pin.center=CGPointMake(l2Pin.center.x -20, l2Pin.center.y -20);
        l2PinHit=TRUE;

    }
    if (CGRectIntersectsRect(l3Pin.frame, l4Pin.frame)) {
        l3Pin.center=CGPointMake(l3Pin.center.x -20, l3Pin.center.y -20);
        l3PinHit=TRUE;

    }
    if (CGRectIntersectsRect(l4Pin.frame, mPin.frame)) {
        l4Pin.center=CGPointMake(l4Pin.center.x -20, l4Pin.center.y -20);
        l4PinHit=TRUE;


    }
    if (CGRectIntersectsRect(r1Pin.frame, r2Pin.frame)) {
        r1Pin.center=CGPointMake(r1Pin.center.x +20, r1Pin.center.y -20);
        r1PinHit=TRUE;
        
    }
    if (CGRectIntersectsRect(r2Pin.frame, r3Pin.frame)) {
        r2Pin.center=CGPointMake(r2Pin.center.x +20, r2Pin.center.y -20);
        r2PinHit=TRUE;
        
    }
    if (CGRectIntersectsRect(r3Pin.frame, r4Pin.frame)) {
        r3Pin.center=CGPointMake(r3Pin.center.x +20, r3Pin.center.y -20);
        r3PinHit=TRUE;
        
    }
    if (CGRectIntersectsRect(r4Pin.frame, mPin.frame)) {
        r4Pin.center=CGPointMake(r4Pin.center.x +20, r4Pin.center.y -20);
        r4PinHit=TRUE;
    }


}

-(void) hidePins{
    if (r1PinHit && !(r1Pin.hidden)) {
        r1Pin.hidden=TRUE;
        //r1Pin=nil;
        x++;
    }
    
    if (r2PinHit && !(r2Pin.hidden)) {
        r2Pin.hidden=TRUE;
        //r2Pin=nil;
        x++;
    }
    
    if (r3PinHit && !(r3Pin.hidden)) {
        r3Pin.hidden=TRUE;
        //r3Pin=nil;
        x++;
    }
    
    if (r4PinHit && !(r4Pin.hidden)) {
        r4Pin.hidden=TRUE;
        //r4Pin=nil;
        x++;
    }
    
    if (mPinHit && !(mPin.hidden)) {
        mPin.hidden=TRUE;
        //mPin=nil;
        x++;
    }
    
    if (l1PinHit && !(l1Pin.hidden)) {
        l1Pin.hidden=TRUE;
        //l1Pin=nil;
        x++;
    }
    
    if (l2PinHit && !(l2Pin.hidden)) {
        l2Pin.hidden=TRUE;
        //l2Pin=nil;
        x++;
    }
    
    if (l3PinHit && !(l3Pin.hidden)) {
        l3Pin.hidden=TRUE;
        //l3Pin=nil;
        x++;
    }
    
    if (l4PinHit && !(l4Pin.hidden)) {
        l4Pin.hidden=TRUE;
        //l4Pin=nil;
        x++;
    }
}
-(void) changelabel
{
    score.text= [NSString stringWithFormat:@"%d",x];
}


@end
